/**
 * 
 * Copyright (C) 2015 by NIP Team
 *
 * @id: TimeOject.java, Mar 13, 2015
 *
 */
package com.nip.commons.job.objects;

import java.util.HashSet;
import java.util.Set;

/**
 * @author ThangNM<thangnm@elcom.com.vn>
 * 
 * @version v
 */
public class TimeObject {

    private final boolean isAlways;
    private final Set<Integer> listSchedule;
    private final String patterm = "[0-9]{1,2}->[1-9]{1,2}";

    /**
     * 
     */
    public TimeObject(String[] listTime, boolean isAlways) {
        listSchedule = new HashSet<Integer>();
        this.isAlways = isAlways;

        if (isAlways)
            return;
        for (String time : listTime) {

            if (time.trim().matches(patterm)) {
                String[] times = time.trim().split("->");
                int begin = Integer.valueOf(times[0]);
                int end = Integer.valueOf(times[1]);

                if (end < begin) {
                    continue;
                }

                for (int i = begin; i <= end; i++) {
                    listSchedule.add(i);
                }
                continue;
            }

            listSchedule.add(Integer.valueOf(time));
        }
    }

    /**
     * Check schedule time
     * 
     * @param time
     * @return
     */
    public boolean checkSchedule(int time) {
        if (isAlways)
            return true;

        return listSchedule.contains(time);
    }

}
